//
//  UIImage+Extension.swift
//  照片选择
//
//  Created by 吴婷婷 on 15/11/17.
//  Copyright © 2015年 wutingting. All rights reserved.
//

import UIKit
extension UIImage {
    func scaleToWidth(width: CGFloat) -> UIImage {
        if width > size.width {
            return self
        }
        
        let height = width * size.height / size.width
        let rect = CGRectMake(0, 0, width, height)
        //1.开启位图上下文
        UIGraphicsBeginImageContext(rect.size)
        //2.绘图
        self.drawInRect(rect)
        //3.获取结果
        let result = UIGraphicsGetImageFromCurrentImageContext()
        //4.关闭上下文
        UIGraphicsEndImageContext()
        
        return result
    }
}
